"""
@File    :   router.py
@Time    :   2024/02/07 10:46:23
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   后台租户相关路由
"""

from typing import Annotated

from fastapi import APIRouter, Depends

from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    DependVerifyAccessTokenMenumPermission,
)
from src.business.admin.system.tenant.tenant.schemas.out import (
    TenantCompleteOutSchema,
    TenantLoginPageSelectOutSchema,
)
from src.enums.permission_operation import PermissionOperationEnum
from src.utils.unified_response import SuccessApiJSONResponse

from .dependencies.route_depend import (
    route_depend_create_tenant,
    route_depend_delete_tenant,
    route_depend_get_tenant,
    route_depend_list_login_page_select_tenant,
    route_depend_list_tenant,
    route_depend_update_tenant,
    route_depend_update_tenant_status,
)

# 构建权限验证相关的依赖项
# 查
depend_select_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.select.value
)
# 增
depend_create_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.create.value
)
# 改
depend_update_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.update.value
)
# 删
depend_delete_permission_verify = DependVerifyAccessTokenMenumPermission(
    __file__, PermissionOperationEnum.delete.value
)


# 创建路由
router = APIRouter(
    prefix="/admin/system/tenant",
    tags=[
        "后台租户接口",
    ],
)


@router.get("/get", summary="查询单个租户")
async def route_get_tenant(
    # 这边需要一个权限验证的依赖(本路由菜单权限标识为: admin@system@tenant@tenant:get)
    verify_menu_permission: Annotated[None, Depends(depend_select_permission_verify)],
    tenant: Annotated[TenantCompleteOutSchema, Depends(route_depend_get_tenant)],
):
    return SuccessApiJSONResponse(data=tenant)


@router.get("/login-page-select-list", summary="查询登录页面使用的后台租户选择列表")
async def route_list_login_page_select_tenant(
    # 由登录页面使用, 因此无需权限验证
    login_page_select_tenant_list: Annotated[
        list[TenantLoginPageSelectOutSchema],
        Depends(route_depend_list_login_page_select_tenant),
    ],
):
    return SuccessApiJSONResponse(data=login_page_select_tenant_list)


@router.post("/list", summary="查询租户列表")
async def route_list_tenant(
    verify_menu_permission: Annotated[None, Depends(depend_select_permission_verify)],
    list_result: Annotated[
        tuple[list[TenantCompleteOutSchema], int], Depends(route_depend_list_tenant)
    ],
):
    search_pagination_result, search_total = list_result
    data = {"result": search_pagination_result, "total": search_total}
    return SuccessApiJSONResponse(data=data)


@router.post("/create", summary="创建租户")
async def route_create_tenant(
    # 这边需要一个权限验证的依赖(本菜单权限标识为: admin@system@tenant@tenant:create)
    verify_menu_permission: Annotated[None, Depends(depend_create_permission_verify)],
    created_id: Annotated[int, Depends(route_depend_create_tenant)],
):
    data = {"created_id": created_id}
    return SuccessApiJSONResponse(message="创建租户成功", data=data)


@router.post("/update", summary="修改租户")
async def route_update_tenant(
    verify_menu_permission: Annotated[None, Depends(depend_update_permission_verify)],
    update_operation: Annotated[None, Depends(route_depend_update_tenant)],
):
    return SuccessApiJSONResponse(message="修改租户成功")


@router.post("/update-status", summary="修改租户状态")
async def route_update_tenant_status(
    verify_menu_permission: Annotated[None, Depends(depend_update_permission_verify)],
    update_status_operation: Annotated[
        None, Depends(route_depend_update_tenant_status)
    ],
):
    return SuccessApiJSONResponse(message="修改租户状态成功")


@router.post("/delete", summary="删除租户")
async def route_delete_tenant(
    verify_menu_permission: Annotated[None, Depends(depend_delete_permission_verify)],
    delete_operation: Annotated[None, Depends(route_depend_delete_tenant)],
):
    return SuccessApiJSONResponse(message="删除租户成功")
